﻿using System;

namespace Framework
{
    public abstract class Behavior<T>
    {
        public event Action Started;
        public event Action Ended;
        public TaskStatus status = TaskStatus.Invalid;

        public abstract TaskStatus Update(T context);
        
        public virtual void Invalidate()
        {
            status = TaskStatus.Invalid;
        }

        public virtual void OnStart(T context)
        {
            
        }

        public virtual void OnEnd(T context)
        {
            
        }

        public TaskStatus Tick(T context)
        {
            if (status == TaskStatus.Invalid)
            {
                OnStart(context);
                Started.SafeInvoke();
            }
            status = Update(context);
            if (status != TaskStatus.Running)
            {
                OnEnd(context);
                Ended.SafeInvoke();
            }
            return status;
        }
    }
}
